/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.css
{

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */

public class CSSLexer {

	//-------------------------------------------------------------------------
	//
	//   Class constants
	//
	//-------------------------------------------------------------------------

	public static const EOF:			String = String.fromCharCode(0);

	public static const SEMI_COLON:		String = ';';

	public static const COLON:			String = ':';

	public static const LEFT_PAREN:		String = '(';

	public static const RIGHT_PAREN:	String = ')';

	public static const LEFT_BRACE:		String = '{';

	public static const RIGHT_BRACE:	String = '}';

	public static const LEFT_BRACKET:	String = '[';

	public static const RIGHT_BRACKET:	String = ']';

	public static const AT:				String = '@';

	public static const HASH:			String = '#';

	public static const DASH:			String = '-';

	public static const PLUS:			String = '+';

	public static const UNDER_SCORE:	String = '_';

	public static const DOUBLE_QUOTE:	String = '"';

	public static const SINGLE_QUOTE:	String = "'";

	public static const SLASH:			String = "/";

	public static const ASTERISK:		String = "*";

	public static const DOT:			String = ".";

	public static const LEFT_ANGLE:		String = "<";

	public static const RIGHT_ANGLE:	String = ">";

	public static const BANG:			String = "!";

	public static const BAR:			String = "|";

	public static const PERCENT:		String = "%";


	private static const RE_LETTER:		RegExp = /[a-z]/i;

	private static const RE_DIGIT:		RegExp = /[0-9]/;

	private static const RE_WHITE:		RegExp = /[ \t\r\n\f]/;

	private static const RE_STRING:		RegExp = /[^\n\r\f\\"]/;


	public static const LEX_WHITE:		String = '<!S!>';

	public static const LEX_DELIM:		String = '<!DELIM!>';

	public static const LEX_ERROR:		String = '<!ERROR!>';

	public static const LEX_COMMENT:	String = '<!COMMENT!>';

	public static const LEX_CDO:		String = '<!CDO!>';

	public static const LEX_CDC:		String = '<!CDC!>';

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor 
	 */
	public function CSSLexer() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  source
	//---------------------------------
	/**
	 * @private
	 */
	private var _source:	String = '';

	public function get source():String {
		return this._source;
	}

	public function set source(value:String):void {
		this._source = value ? value + EOF : EOF;
		this._char = this._source.charAt(this._position++);
	}

	//---------------------------------
	//  lexem
	//---------------------------------
	/**
	 * @private
	 */
	private var _lexem:		String;

	public function get lexem():String {
		return this._lexem;
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	private var _position:	uint;

	private var _char:		String;

	//-------------------------------------------------------------------------
	//
	//   Public methods
	//
	//-------------------------------------------------------------------------

	public function clear():void {
		this._position = 0;
		this._source = null;
	}

	public function readLexem():void {

		if ( this._char === EOF ) {
			this._lexem = this._char;
		}

		else if ( RE_WHITE.test(this._char) ) {
			this._lexem = LEX_WHITE;
			this._char = this._source.charAt(this._position++);
		}

		else if ( this._char === DOUBLE_QUOTE ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_string1();
		}

		else if ( this._char === SINGLE_QUOTE ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_string2();
		}

		else if ( this._char === AT ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_nmstart();
		}

		else if ( this._char === SLASH ) {
			this._char = this._source.charAt(this._position++);
			this.read_comment();
		}

		else if (
			   this._char === LEFT_PAREN
			|| this._char === RIGHT_PAREN
			|| this._char === LEFT_BRACKET
			|| this._char === RIGHT_BRACKET
			|| this._char === LEFT_BRACE
			|| this._char === RIGHT_BRACE
			|| this._char === SEMI_COLON
			|| this._char === COLON
			|| this._char === BAR
		) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
		}

		// identifier or number or CDC
		else if ( this._char === DASH ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_number_or_ident_or_cdc();
		}

		// identifier
		else if (
			   RE_LETTER.test(this._char)
			|| this._char === UNDER_SCORE
		) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_nmstart();
		}

		else if ( this._char === DOT ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_float();
		}

		else if ( this._char === PLUS ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_number();
		}

		else if ( RE_DIGIT.test(this._char) ) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_number();
		}

		else if ( this._char === LEFT_ANGLE ) {
			this._char = this._source.charAt(this._position++);
			this.read_CDO();
		}

		else {
			this._lexem = LEX_DELIM + ' <' + this._char + '>';
			this._char = this._source.charAt(this._position++);
		}

	}

	/**
	 * 
	 * 
	 */
	public function read_URI():void {

		while ( this._char !== LEFT_PAREN ) {
			this._char = this._source.charAt(this._position++);
		}
		
		this._lexem = this._char;

		this._char = this._source.charAt(this._position++);

		while ( this._char !== RIGHT_PAREN) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
		}

		this._lexem += this._char;
		this._char = this._source.charAt(this._position++);

	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 * 
	 */	
	private function read_CDO():void {
		if ( this._char === BANG ) {

			this._char = this._source.charAt(this._position++);

			if ( this._char === DASH ) {

				this._char = this._source.charAt(this._position++);

				if ( this._char === DASH ) {

					this._lexem = LEX_CDO;
					this._char = this._source.charAt(this._position++);

				} else {
					this._lexem = LEX_ERROR;
					this._char = this._source.charAt(this._position++);
				}

			} else {
				this._lexem = LEX_ERROR;
				this._char = this._source.charAt(this._position++);
			}

		} else {
			this._lexem = LEX_ERROR;
			this._char = this._source.charAt(this._position++);
		}

	}

	/**
	 * Целое или действительное
	 */	
	private function read_number():void {

		if ( RE_DIGIT.test(this._char) ) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);

			while ( RE_DIGIT.test(this._char) ) {
				this._lexem += this._char;
				this._char = this._source.charAt(this._position++);
			}

			if ( this._char === DOT ) {
				this.read_float();
			}
		}

		else if ( this._char === DOT ) {
			this.read_float();
		}

		else {
			this._lexem = LEX_ERROR;
			this._char = this._source.charAt(this._position++);
		}
	}

	/**
	 * 
	 * 
	 */
	private function read_float():void {
		this._lexem += this._char;
		this._char = this._source.charAt(this._position++);

		if ( RE_DIGIT.test(this._char) ) {

			do {
				this._lexem += this._char;
				this._char = this._source.charAt(this._position++);
			} while ( RE_DIGIT.test(this._char) )

		} else {
			this._lexem = LEX_ERROR;
			this._char = this._source.charAt(this._position++);
		}

	}

	/**
	 * 
	 * 
	 */
	private function read_number_or_ident_or_cdc():void {

		if ( this._char === DOT ) {
			this.read_float();
		}

		else if ( RE_DIGIT.test(this._char) ) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
			this.read_number();
		}

		// identifier
		else if (
			   RE_LETTER.test(this._char)
			|| this._char === UNDER_SCORE
		) {
			this._lexem = this._char;
			this._char = this._source.charAt(this._position++);
			this.read_nmstart();
		}

		else if ( this._char === DASH ) {

			this._char = this._source.charAt(this._position++);

			if ( this._char === RIGHT_ANGLE ) {
				this._lexem = LEX_CDC;
			} else {
				this._lexem = LEX_ERROR;
			}

			this._char = this._source.charAt(this._position++);			
		}
	}

	/**
	 * 
	 * 
	 */
	private function read_nmstart():void {
		if (
			   this._char === UNDER_SCORE
			|| RE_LETTER.test(this._char)
		) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
			this.read_nmchar();
		} else {
			this._char = this._source.charAt(this._position++);
		}
	}

	/**
	 * 
	 * 
	 */
	private function read_nmchar():void {
		while ( 
			   this._char === UNDER_SCORE
			|| RE_LETTER.test(this._char)
			|| RE_DIGIT.test(this._char)
			|| this._char === DASH 
		) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
		}
		
		if ( this._char === LEFT_PAREN ) {
			
		}

	}

	/**
	 * 
	 * 
	 */
	private function read_string1():void {
		while ( 
			RE_STRING.test(this._char)
		) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
		}

		if (this._char === DOUBLE_QUOTE ) {
			this._lexem += this._char;
		} else {
			this._lexem = LEX_ERROR;
		}

		this._char = this._source.charAt(this._position++);
	}

	/**
	 * 
	 * 
	 */
	private function read_string2():void {
		while ( 
			RE_STRING.test(this._char)
		) {
			this._lexem += this._char;
			this._char = this._source.charAt(this._position++);
		}

		if (this._char === SINGLE_QUOTE ) {
			this._lexem += this._char;
		} else {
			this._lexem = LEX_ERROR;
		}

		this._char = this._source.charAt(this._position++);
	}

	/**
	 * 
	 * 
	 */
	private function read_comment():void {
		if ( this._char === ASTERISK ) {

			this._char = this._source.charAt(this._position++);

			while ( 
				this._char !== ASTERISK
			) {
				this._char = this._source.charAt(this._position++);
			}

			this._char = this._source.charAt(this._position++);

			if ( this._char === SLASH ) {
				this._lexem = LEX_COMMENT;
			} else {
				this._lexem = LEX_ERROR;
			}

		} else {
			this._lexem = LEX_ERROR;
		}

		this._char = this._source.charAt(this._position++);
	}


}
}
